package com.gg.zjj.controller;

import com.gg.common.pojo.MManufacture;
import com.gg.common.pojo.MProcedure;
import com.gg.common.pojo.MProcedureModule;
import com.gg.zjj.pojo.DFileMApplyView;
import com.gg.zjj.service.DevelopProductionDispatchOrdersService;
import com.gg.zjj.util.Result;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author koco
 * 2022/10/14
 * 14:40
 */
@Controller
@RequestMapping("/zjj/")
public class DevelopProductionDispatchOrders {

    @Resource
    private DevelopProductionDispatchOrdersService service;

    @RequestMapping("queryUndispatchedProductionSchedules.action")
    @ResponseBody
    private Result<List<DFileMApplyView>> queryDFileList() {
        return Result.success(service.queryUndispatchedProductionSchedulesService());
    }

    @RequestMapping("checkProcessMaterialIsItDone.action")
    @ResponseBody
    private Result checkProcessMaterialIsItDone(@RequestBody String v) throws Exception {
        return service.checkProcessMaterialIsItDoneService(v);
    }


    @RequestMapping("submitProductionDispatch.action")
    @ResponseBody
    private Result submitProductionDispatch(@RequestBody String val) {
        MManufacture o = (MManufacture) JSONObject.toBean(JSONObject.fromObject(val), MManufacture.class);
        List<MProcedure> procedureList = new ArrayList<>();
        for (int i = 0; i < o.getProductionProcesses().size(); i++) {
            List<MProcedureModule> moduleList = new ArrayList<>();
            MProcedure procedure = (MProcedure) JSONObject.toBean(JSONObject.fromObject(o.getProductionProcesses().get(i)), MProcedure.class);
            procedureList.add(procedure);
            for (int j = 0; j < procedure.getProductionMaterial().size(); j++) {
                MProcedureModule procedureModule = (MProcedureModule) JSONObject.toBean(JSONObject.fromObject(procedureList.get(i).getProductionMaterial().get(j)), MProcedureModule.class);
                moduleList.add(procedureModule);
            }
            procedureList.get(i).setProductionMaterial(moduleList);
        }
        o.setProductionProcesses(procedureList);
        o.setRegisterTime(o.getRegisterTime().replace("T", " "));
        return service.submitProductionDispatchService(o);
    }
}
